import { shoppingCartAdd } from '@/api/goods'
import { objectToQuery } from '@/utils/util'
import router from '@/router'
import { useUserStore } from '@/stores/user'

export const useSubmitEffect = () => {
    const userStore = useUserStore()
    
    // 加入购物车
    const cartHandle = async ({ spec }: any) => {
        try {
            if (!spec || !spec.id) {
                throw new Error('商品规格信息不完整')
            }
            await shoppingCartAdd({
                goodsSkuId: spec.id,
                num: spec.number || 1
            })
            //uni.$u.toast('加入购物车成功')
            if (userStore && userStore.getShopCartCount) {
                await userStore.getShopCartCount()
            }
        } catch (error) {
            console.log('添加购物车', error)
            throw error
        }
    }

    // 处理普通购买
    const buyHandle = async ({ spec }: any, goodsId: number) => {
        try {
            const buyGoods = [{
                goodsId: goodsId,
                skuId: spec.id,
                num: spec.number,
                cartId: 0
            }]
            router.navigateTo({
                path: '/pages/goods_order/goods_order',
                query: {
                    buyGoods: JSON.stringify(buyGoods),
                    buyType: 'buyNow'
                }
            })
        } catch (error) {
            console.log('buyHandle 路由跳转失败', error)
            // 降级使用 uni.navigateTo
            try {
                const buyGoods = [{
                    goodsId: goodsId,
                    skuId: spec.id,
                    num: spec.number,
                    cartId: 0
                }]
                uni.navigateTo({
                    url: `/pages/goods_order/goods_order?buyGoods=${encodeURIComponent(JSON.stringify(buyGoods))}&buyType=buyNow`
                })
            } catch (e) {
                console.log('uni.navigateTo 也失败', e)
                uni.$u.toast('页面跳转失败')
            }
        }
    }

    // 秒杀抢购
    const buyNowHandle = async ({ spec }: any, goodsId: number, seckillId: number) => {
        try {
            const buyGoods = [{
                goodsId: goodsId,
                skuId: spec.id,
                num: spec.number,
                cartId: 0,
                seckillId: seckillId
            }]
            router.navigateTo({
                path: '/pages/goods_order/goods_order',
                query: {
                    buyGoods: JSON.stringify(buyGoods),
                    buyType: 'buyNow'
                }
            })
        } catch (error) {
            console.log('buyNowHandle 路由跳转失败', error)
            // 降级使用 uni.navigateTo
            try {
                const buyGoods = [{
                    goodsId: goodsId,
                    skuId: spec.id,
                    num: spec.number,
                    cartId: 0,
                    seckillId: seckillId
                }]
                uni.navigateTo({
                    url: `/pages/goods_order/goods_order?buyGoods=${encodeURIComponent(JSON.stringify(buyGoods))}&buyType=buyNow`
                })
            } catch (e) {
                console.log('uni.navigateTo 也失败', e)
                uni.$u.toast('页面跳转失败')
            }
        }
    }

    return {
        cartHandle,
        buyHandle,
        buyNowHandle
    }
}
